<?php require_once 'Master_controller.php';
class Contact extends Master_controller {
	protected $_page_uri = "contact";
	private $_contact_email;
	
	function __construct() {
		parent::__construct();
		$this->load->library(array('validation'));
		$this->load->helper('captcha');
		$this->view_data['active_contact'] = "current";
		$this->view_data['view'] = "contact_view";
		$this->_page_uri = site_url() . $this->_page_uri;
		$this->_contact_email = $this->Config_model->get_by_key(CONTACT_EMAIL)->row()->value;
	}
	
	function index() {
		$this->view_data['contact_address'] = $this->Config_model->get_by_key(CONTACT_ADDRESS)->row()->value;
		$this->view_data['contact_phone'] = $this->Config_model->get_by_key(CONTACT_PHONE)->row()->value;
		$this->view_data['contact_fax'] = $this->Config_model->get_by_key(CONTACT_FAX)->row()->value;
		$this->view_data['contact_email'] = $this->_contact_email;
		$this->view_data['action'] = $this->_page_uri . '/index';
		
		// set validation properties
		$this->_set_fields();
		$this->_set_rules();
		
		// run validation
		if ($this->validation->run() == FALSE) {
			$this->view_data['message'] = '';
		} else if (strtoupper($this->session->userdata('captcha')) == strtoupper($this->input->post('captcha'))) {
			$config = Array(
				'protocol' => 'smtp',
			  	'smtp_host' => EMAIL_SMTP_HOST,
			  	'smtp_port' => EMAIL_SMTP_PORT,
			  	'smtp_user' => EMAIL_SMTP_USER,
			  	'smtp_pass' => EMAIL_SMTP_PASS,
			  	'mailtype' => 'html',
			  	'charset' => 'utf-8',
			  	'wordwrap' => TRUE
			);
		 	$this->load->library('email', $config);
			$this->email->set_newline("\n");
			$this->email->from($this->input->post('email'));
			$this->email->to($this->_contact_email);
			$this->email->subject($this->input->post('title'));
			$this->email->message($this->input->post('content').$this->input->post('name'));
		 	
			if($this->email->send())
			{
				$this->view_data['message'] = $this->lang->line('email-sent');
			}
		 	else
			{
		 		$this->view_data['message'] = $this->email->print_debugger();
			}
		} else {
			$this->view_data['message'] = $this->lang->line('code-not-match');
		}
		
		// create captcha
		$vals = array (
		    'img_path' => './captcha/',
		    'img_url' => site_url().'captcha/',
		    'font_path' => './captcha/fonts/3.ttf',
		    'img_width' => 150,
		    'img_height' => 40,
		    'expiration' => 7200
		);
		
		$cap = create_captcha($vals);
		$this->view_data['captcha_image'] = $cap['image'];
		$this->session->set_userdata('captcha', $cap['word']);
		
		$this->render_page($this->view_data);
	}
	
	// validation fields
	function _set_fields() {
		$fields['title'] = 'title';
		$fields['name'] = 'name';
		$fields['email'] = 'email';
		$fields['content'] = 'content';
		$fields['captcha'] = 'captcha';
		
		$this->validation->set_fields($fields);
	}
	//validation rules
	function _set_rules() {
		$rules['title'] = 'trim|required';
		$rules['name'] = 'trim|required';
		$rules['email'] = 'trim|required|valid_email';
		$rules['content'] = 'trim|required';
		$rules['captcha'] = 'trim|required';
		$this->validation->set_rules($rules);
		$this->validation->set_message('required', $this->lang->line('required'));
		$this->validation->set_message('isset', $this->lang->line('isset'));
		$this->validation->set_error_delimiters('<span class="error">', '</span>');
	}
}